package com.winchain.chainbill.risk.base.service.impl;

import com.shell.bee.base.utils.DateUtil;
import com.shell.bee.base.utils.StringUtil;
import com.shell.bee.base.utils.SystemUtil;
import com.shell.bee.persistence.annotation.Pagination;
import com.shell.bee.persistence.model.PageLimit;
import com.shell.bee.persistence.service.impl.PersistentServiceImpl;
import com.shell.framework.utils.CommonDataUtil;
import com.shell.framework.utils.ParameterHelper;
import com.shell.governor.modules.vo.ParameterVo;
import com.winchain.chainbill.basic.cus.dao.CusRecDao;
import com.winchain.chainbill.basic.cus.vo.BasCustomerVo;
import com.winchain.chainbill.risk.base.dao.RmIntRateTemplateDao;
import com.winchain.chainbill.risk.base.po.RmIntRate;
import com.winchain.chainbill.risk.base.service.RmIntRateService;
import com.winchain.chainbill.risk.biz.rate.vo.RateVo;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("rmIntRateService")
public class RmIntRateServiceImpl extends PersistentServiceImpl<RmIntRate> implements RmIntRateService {
	@Autowired
	private RmIntRateTemplateDao rmIntRateTemplateDao;
	@Autowired
	private CusRecDao basCustomerDao;

	public List<RateVo> findRateForEnt(String rateBizType, String grpId, String cusId, String finId) {
		ParameterVo sndNeedInterest = ParameterHelper.getCommonParameter("SND_NEED_INTEREST");
		if (sndNeedInterest.getParmValue().equals("0")) {
			List<RateVo> rateVos = this.rmIntRateTemplateDao.findRateForEnt(rateBizType, grpId, cusId, finId);
			for (RateVo rateVo : rateVos) {
				if ((rateVo.getRateCode().equals("snd_fixed_rate"))
						|| (rateVo.getRateCode().equals("snd_variable_rate"))) {
					rateVo.setDisplayable("0");
				}
			}
			return rateVos;
		}
		return this.rmIntRateTemplateDao.findRateForEnt(rateBizType, grpId, cusId, finId);
	}

	public void updateRate(RateVo rateVo) {
		RmIntRate rate = new RmIntRate();
		rate.setGrpId(rateVo.getGrpId());
		rate.setCusId(rateVo.getCusId());
		rate.setFinId(rateVo.getFinId());
		rate.setRateCode(rateVo.getRateCode());
		rate = (RmIntRate) selectOne(rate);
		if (null != rate) {
			rate.setRateValue(rateVo.getRateValue());
			updateNotNull(rate);
		} else {
			rate = new RmIntRate();
			BeanUtils.copyProperties(rateVo, rate);
			rate.setRateState("E");
			rate.setRateId(SystemUtil.getUUID());
			rate.setCreateTime(DateUtil.getSystemDateTime());
			save(rate);
		}
	}

	public RateVo findRateByCode(String rateBizType, String rateCode, String grpId, String cusId, String finId) {
		return this.rmIntRateTemplateDao.findRateByCode(rateBizType, rateCode, grpId, cusId, finId);
	}

	@Pagination
	public List<BasCustomerVo> findCustomerRateForGrp(PageLimit limit, String grpId, String entId, String[] tagCodes,
			String searchValue, String RateTemplateBizType, String[] excCusTypes, String[] bizCodes) {
		BasCustomerVo basCustomerVo = new BasCustomerVo();
		if (StringUtil.empty(entId)) {
			entId = CommonDataUtil.getCurEntId();
		}
		basCustomerVo.setNotTagCodes(excCusTypes);
		basCustomerVo.setTagCodes(tagCodes);
		basCustomerVo.setBizCodes(bizCodes);
		basCustomerVo.setGrpId(grpId);
		basCustomerVo.setCusState("E");
		if (StringUtil.notTrimEmpty(searchValue)) {
			basCustomerVo.setSearchValue(StringUtil.getParameterLike(searchValue));
		}
		List<BasCustomerVo> customers = this.basCustomerDao.findCusForGrp(basCustomerVo);
		Map<String, String> rateMap;
		for (BasCustomerVo c : customers) {
			String cusId = c.getCusId();
			rateMap = new HashMap();
			c.setRates(rateMap);

			List<RateVo> vrates = this.rmIntRateTemplateDao.findRateForEnt(RateTemplateBizType, grpId, cusId, entId);
			for (RateVo rate : vrates) {
				rateMap.put(rate.getRateCode(), rate.getRateValue());
			}
		}
		return customers;
	}
}
